/*
 * @Author: sbotlp
 * @Email: sbotlp@sina.com
 * @Date: 2019-06-29 17:18:16
 * @LastEditTime: 2019-06-30 10:29:44
 * @Description: http://www.sbotlp.com.cn
 */
import { message } from 'antd';
import * as constants from '../constants/ConstantsTypes';
export interface AddBulletConstants {
  type: constants.ADD_BULLET_NUMBER;
  body: number;
}
export interface BachAddBulletConstants {
  type: constants.BATCH_ADD_BULLET_NUMBER;
  body: number;
}
export interface UseBullteConstants {
  type: constants.USE_BULLET_NUMBER;
  body: number;
}
export interface BatchUseBullteConstants {
  type: constants.BATCH_USE_BULLET_NUMBER;
  body: number;
}
export type BulletAction = AddBulletConstants | BachAddBulletConstants | UseBullteConstants| BatchUseBullteConstants;

export function addBulletConstants (data: number) {
  return (dispatch: Function, state: Function) => {
    const { bulletNumber } = state().Bullet;
    dispatch({
      type: constants.ADD_BULLET_NUMBER,
      body: bulletNumber + 1
    });
  }
}
export function bachAddBulletConstants (data: number) {
  return (dispatch: Function, state: Function) => {
    const { bulletNumber } = state().Bullet;
    const newBulletNumber = bulletNumber > 0 ? bulletNumber * 2 : bulletNumber + 1;
    dispatch({
      type: constants.ADD_BULLET_NUMBER,
      body: newBulletNumber
    });
  }
}

export function useBullteAction(data: number) {
  return (dispatch: Function, state: Function) => {
    const { bulletNumber } = state().Bullet;
    if (!bulletNumber) {
      message.error('木有子弹了');
    }
    const newBulletNumber = bulletNumber > 0 ? bulletNumber - 1 : 0;
    dispatch({
      type: constants.USE_BULLET_NUMBER,
      body: newBulletNumber
    });
  }
}
export function batchUseBullteAction(data: number) {
  return (dispatch: Function, state: Function) => {
    const { bulletNumber } = state().Bullet;
    if (!bulletNumber) {
      message.error('木有子弹了');
    }
    const newBulletNumber: any = bulletNumber > 0 ? bulletNumber / 2 : 0;
    dispatch({
      type: constants.BATCH_USE_BULLET_NUMBER,
      body: parseInt(newBulletNumber, 10)
    });
  }
}